﻿using System;
using System.Collections.Specialized;
using System.Net;
using Raven.Client;
using Raven.Client.Connection;
using Raven.Client.Connection.Async;
using Raven.Client.Document;

namespace Sponsorship.ReadModel
{
    public class QueryYourWritesDocumentStoreDecorator : IDocumentStore
    {
        private readonly IDocumentStore _innerDocumentStore;

        public QueryYourWritesDocumentStoreDecorator (IDocumentStore innerDocumentStore)
        {
            _innerDocumentStore = innerDocumentStore;
        }

        public void Dispose()
        {
            _innerDocumentStore.Dispose();
        }

        public bool WasDisposed
        {
            get { return _innerDocumentStore.WasDisposed; }
        }

        public event EventHandler AfterDispose
        {
            add { _innerDocumentStore.AfterDispose += value; }
            remove { _innerDocumentStore.AfterDispose -= value; }
        }

        public IDisposable AggressivelyCacheFor(TimeSpan cahceDuration)
        {
            return _innerDocumentStore.AggressivelyCacheFor (cahceDuration);
        }

        public IDisposable DisableAggressiveCaching()
        {
            return _innerDocumentStore.DisableAggressiveCaching();
        }

        public IDocumentStore Initialize()
        {
            return _innerDocumentStore.Initialize();
        }

        public IAsyncDocumentSession OpenAsyncSession()
        {
            var asyncDocumentSession = _innerDocumentStore.OpenAsyncSession();
            asyncDocumentSession.Advanced.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;
            return asyncDocumentSession;
        }

        public IAsyncDocumentSession OpenAsyncSession(string database)
        {
            var asyncDocumentSession = _innerDocumentStore.OpenAsyncSession (database);
            asyncDocumentSession.Advanced.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;
            return asyncDocumentSession;
        }

        public IDocumentSession OpenSession()
        {
            var documentSession = _innerDocumentStore.OpenSession();
            documentSession.Advanced.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;
            return documentSession;
        }

        public IDocumentSession OpenSession(string database)
        {
            var documentSession = _innerDocumentStore.OpenSession (database);
            documentSession.Advanced.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;
            return documentSession;
        }

        public IDocumentSession OpenSession(string database, ICredentials credentialsForSession)
        {
            var documentSession = _innerDocumentStore.OpenSession (database, credentialsForSession);
            documentSession.Advanced.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;
            return documentSession;
        }

        public IDocumentSession OpenSession(ICredentials credentialsForSession)
        {
            var documentSession = _innerDocumentStore.OpenSession (credentialsForSession);
            documentSession.Advanced.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;
            return documentSession;
        }

        public Guid? GetLastWrittenEtag()
        {
            return _innerDocumentStore.GetLastWrittenEtag();
        }

        public NameValueCollection SharedOperationsHeaders
        {
            get { return _innerDocumentStore.SharedOperationsHeaders; }
        }

        public HttpJsonRequestFactory JsonRequestFactory
        {
            get { return _innerDocumentStore.JsonRequestFactory; }
        }

        public string Identifier
        {
            get { return _innerDocumentStore.Identifier; }
            set { _innerDocumentStore.Identifier = value; }
        }

        public IAsyncDatabaseCommands AsyncDatabaseCommands
        {
            get { return _innerDocumentStore.AsyncDatabaseCommands; }
        }

        public IDatabaseCommands DatabaseCommands
        {
            get { return _innerDocumentStore.DatabaseCommands; }
        }

        public DocumentConvention Conventions
        {
            get { return _innerDocumentStore.Conventions; }
        }

        public string Url
        {
            get { return _innerDocumentStore.Url; }
        }
    }
}